import Vue from 'vue'

/**
 * @description 银行卡号加空格 1111111111111111 => 1111 1111 1111 1111
 */
Vue.filter('initBankNo', function(str) {
    return str.replace(/\s/g, '').replace(/(\d{4})(?=\d)/g, "$1 ")
});

/**
 * @description 银行卡号中间替换为* 1111 **** **** 1111
 */
Vue.filter('hideBankNo', function(str) {
	var reg = /^(\d{4})\d+(\d{4})$/;
    return str.replace(reg, "$1 **** **** $2");
});

/**
 * @description 手机号中间替换为* 18768888888 => 1876****888
 */
Vue.filter('hidePhoneNo', function(str) {
    return str.substr(0,3) + "****" + str.substr(7)
});

// 时间戳转日期(1569736900 => 2019-09-29)
Vue.filter('timeInit', function(str, type='ymdhm') {
    if (!str) {
        return '- -'
    }
    str = String(str).length <= 10 ? str * 1000 : str;
    let formatTime = (a) => parseInt(a) < 10 ? '0' + parseInt(a) : parseInt(a);
    let t = new Date(str);
    let year = t.getFullYear();
    let month = formatTime(t.getMonth() + 1);
    let day = formatTime(t.getDate());
    let hours = formatTime(t.getHours());
    let minutes = formatTime(t.getMinutes());
    let seconds = formatTime(t.getSeconds());
    if (type == "ymd") {
        return `${year}-${month}-${day}`;
    } else if (type == 'hms') {
        return `${hours}:${minutes}:${seconds}`;
    } else if (type == 'ms') {
        return `${minutes}:${seconds}`;
    } else if (type == 'ymdhm') {
        return `${year}-${month}-${day} ${hours}:${minutes}`;
    } else if (type == "article") {
        //6月13号 13:00
        return `${month}-${day} ${hours}:${minutes}`

    } else if (type == "order") {
        //6月13号 13:00
        return `${year}年${month}月${day}号`

    } else {
        return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
    }
})